package com.briup.jz.service.impl;

import com.briup.jz.bean.BaseFile;
import com.briup.jz.bean.BaseFileExample;
import com.briup.jz.dao.BaseFileMapper;
import com.briup.jz.service.IBaseFileService;
import com.briup.jz.utils.CustomerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BaseFileServiceImpl implements IBaseFileService {

    //依赖注入
    @Resource
    private BaseFileMapper baseFileMapper;


    @Override
    public void saveOrUpdat(BaseFile baseFile) throws CustomerException {
        if (baseFile.getId()!=null){
            //执行更新操作
            baseFileMapper.updateByPrimaryKey(baseFile);

        }else {
            //如果id等于空就执行插入操作
           BaseFileExample example= new BaseFileExample();
           BaseFileExample.Criteria criteria =example.createCriteria();
           criteria.andExtNameEqualTo(baseFile.getExtName());
           //通过模板查询是否名称已经存在
            List<BaseFile> list =baseFileMapper.selectByExample(example);
            //判断集合成员
            if (list.size()>0){
                //抛出异常
                throw new CustomerException("类目同名");
            }
            //执行插入操作
            baseFileMapper.insert(baseFile);

        }


    }



    @Override
    public BaseFile query(String id) {
        //

        return baseFileMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(String id) throws CustomerException {

        //当id存在才执行删除操作,id在数据库中
        BaseFile baseFile=baseFileMapper.selectByPrimaryKey(id);
        if (baseFile==null){
            //抛出异常
            throw new CustomerException("该id不存在");
        }
        //执行山粗操作
        baseFileMapper.deleteByPrimaryKey(id);

    }
}
